package smartcounters.androidclient;

import smartcounters.androidclient.WebServices.InformationReaderWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.AccountInfo;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.Fault;
import smartcounters.androidclient.model.eOperationsEnum;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.graphics.drawable.Drawable;
import android.os.Bundle;	
import android.os.Message;

public class FaultsAndAccountsActivity extends MapActivity implements OnResultReceiver{
	private Fault[] m_OpenFaults;
	private AccountInfo[] m_Accounts;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.faults_and_accounts_layout);
        
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        MapController mapController = mapView.getController();
        
        // GeoPoint of Israel
        float x =(float) 31.574469; 
        float y =(float) 34.813242; 
        
        GeoPoint point = new GeoPoint((int)(x*1E6), (int)(y*1E6));
        mapController.animateTo(point);
        mapController.setZoom(9);
        
        InformationReaderWebServiceClient client = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
		client.GetAllOpenFaults(Configuration.getInstance().getUserCredentials());
		client.RegisterResultReceiver(this);

    }
    
    @Override
    protected boolean isRouteDisplayed() {
    	return false;
    }

	public void ReceiveResult(Message i_Msg) {
		if (i_Msg.obj != null) {
			if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetAllOpenFaults) {
				m_OpenFaults = (Fault[])i_Msg.obj;
				
				InformationReaderWebServiceClient client = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
				client.GetAllAccounts(Configuration.getInstance().getUserCredentials());
				client.RegisterResultReceiver(this);
			}
			if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetAllAccounts) {
				m_Accounts = (AccountInfo[])i_Msg.obj;
				showMarkers(m_OpenFaults,m_Accounts);
			}
		}
	}
	
	private void showMarkers(Fault[] m_OpenFaults, AccountInfo[] m_Accounts)
	{	
		
		MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        //**********************************************************************************
        // properties for Marker_account
        MyItemizedOverlay myItemizedOverlayAccount = null;
        MyLocationOverlay myLocationOverlayAccount = null;
            
        Drawable marker_account = getResources().getDrawable(R.drawable.account);
        
        myItemizedOverlayAccount = new MyItemizedOverlay(marker_account,this);
        mapView.getOverlays().add(myItemizedOverlayAccount);
        
        //**********************************************************************************
        // properties for Marker_faults
        MyItemizedOverlay myItemizedOverlayFaults = null;
        MyLocationOverlay myLocationOverlayFaults = null;
        
        Drawable marker_faults = getResources().getDrawable(R.drawable.fault);
        
        myItemizedOverlayFaults = new MyItemizedOverlay(marker_faults,this);
        mapView.getOverlays().add(myItemizedOverlayFaults);
        //**********************************************************************************

        Fault foundFaultOfAccount = null;
        
        for (AccountInfo account : m_Accounts)
        {
        	int AccoundID = account.getAccountID();
        	foundFaultOfAccount = null;
            for (Fault fault : m_OpenFaults)
            {
            	if (AccoundID == fault.getAccountID())
            		foundFaultOfAccount = fault;
            }
            
        	double Latitude = account.getLatitude();
        	double Longitude = account.getLongitude();
        	
            //GeoPoint
            GeoPoint point = new GeoPoint((int)(Latitude*1E6),(int)(Longitude*1E6));
            
            if (foundFaultOfAccount != null)
            {
            	myItemizedOverlayFaults.addItem(
            			point,
            			account.getCity(),
            			"Street: " + account.getStreet() + "\nHouse: " + account.getHouse()  +"\nApartment: "+ account.getApartment() + "\nPhone: " + account.getPhone() + "\nFault: " + foundFaultOfAccount.getDescription());
	            myLocationOverlayFaults = new MyLocationOverlay(this, mapView);
	            mapView.getOverlays().add(myLocationOverlayFaults);
	            mapView.getOverlays().add(myItemizedOverlayFaults);

            }
            else
            {  
            	myItemizedOverlayAccount.addItem(
            			point,
            			account.getCity(),
            			"Street: " + account.getStreet() + "\nHouse: " + account.getHouse() +"\nApartment: " + account.getApartment() + "\nPhone: " + account.getPhone());
	            myLocationOverlayAccount = new MyLocationOverlay(this, mapView);
	            mapView.getOverlays().add(myLocationOverlayAccount);
	            mapView.getOverlays().add(myItemizedOverlayAccount);
            }
            
            mapView.postInvalidate();
        }
	}
}








